package com.wongoing.sys.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.wongoing.sys.model.SysLog;
import com.wongoing.util.IpUtil;
import com.wongoing.util.SubjectUtil;

/**
 * 类功能说明 TODO:常用常量定义 类修改者 修改日期 修改说明
 * 
 * @author yuany yuany@mesnac.com
 * @date 2016-5-20 17:21:14
 * @version V1.0
 */

public class Constants extends com.wongoing.util.Constants{
	//激活状态码
	public static Integer IS_NOT_ACTIVED = 1;
	public static Integer IS_ACTIVED = 0;
	/*	public static ShiroUser getCurrendUser(){
		ShiroUser s = new ShiroUser(1, "admin");
		try {
			Subject subject = SecurityUtils.getSubject();
			s = (ShiroUser) subject.getSession().getAttribute(
					SHIRO_USER);
		} catch (Exception e) {
			s = new ShiroUser(0, "APP用户");
		}
		return s;
	}  */
	
	
	
	/**
	 * 功能说明：获取日志记录信息
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月13日 上午11:41:48
	 * @param session
	 * @param o
	 * @param type 操作类型：1：安全日志，2：操作日志
	 * @param stmtId
	 * @param eventName
	 */
	 public static <T> void getLogs(SqlSession session, T o,Integer type,
			 String stmtId, String eventName)
	 {
		 if (!LOGS_TB_NAME.equals(o.getClass().getSimpleName()))
		 {
			 String ip = getIpAddr();
			 String[] sdf = getFiledName(o);
			 String params = "";
			 for (String str : sdf) {
				 Object value = getFieldValueByName(str, o);
				 params += str + ":" +(value==null?"null":value.toString())+";";
				 System.out.println(params);
			 }
			 Object idObj = getFieldValueByName(sdf[0], o);
			 String id = idObj==null?"null":idObj.toString();
			 SysLog l=new SysLog();			 
			 l.setUserId(SubjectUtil.getCurrentUser().getUserId());
			 l.setParams(params);
			 l.setLogDate(new Date());
			 l.setEventName(eventName);
			 l.setEventRecord(o.getClass().getName());
			 l.setLocal(IpUtil.getAddressByIP(ip));
			 l.setObjectId(id);
			 l.setType(type);
			 l.setIp(ip);
			 session.insert(stmtId,l);
		 }
	 }
	 /**
	  * <p>Title: getBatch</p>
	  * <p>Description:</p>
	  * <p>Company: mesnac</p>
	  * @param session
	  * @param list
	  * @param type
	  * @param stmtId
	  * @param eventName
	  * @author wuzq
	  * @date 2017-5-25上午9:54:16
	  * @retrun
	  */
	 public static <T> void getBatch(SqlSession session, List<T> list,Integer type,
			 String stmtId, String eventName){
		 List<SysLog> logBatch =  new ArrayList<SysLog>();
		 for (T t : list) {
			 String[] sdf = getFiledName(t);
			 String params = "";
			 for (String str : sdf) {
				 Object value = getFieldValueByName(str, t);
				 params += str + ":" +(value==null?"null":value.toString())+";";
			 }
			 String ip = getIpAddr();
			 Object idObj = getFieldValueByName(sdf[0], t);
			 String id = idObj==null?"null":idObj.toString();
			 SysLog l=new SysLog();			 
			 l.setUserId(SubjectUtil.getCurrentUser().getUserId());
			 l.setParams(params);
			 l.setLogDate(new Date());
			 l.setEventName(eventName);
			 l.setEventRecord(t.getClass().getName());
			 l.setLocal(IpUtil.getAddressByIP(ip));
			 l.setObjectId(id);
			 l.setType(type);
			 l.setIp(ip);
			 logBatch.add(l);
		}
		 session.insert(stmtId,logBatch);
	 }
	
	 /**
	  * <p>Title: </p>
	  * <p>Description:</p>
	  * <p>Company: mesnac</p>
	  * @param session
	  * @param ids
	  * @param type
	  * @param stmtId
	  * @param eventName
	  * @author wuzq
	  * @date 2017-5-25上午10:20:55
	  * @retrun
	  */
	public static <Serializable> void getDeleteBatchLog(SqlSession session, List<Serializable> ids,Integer type,
			 String stmtId, String eventName){
		 List<SysLog> logBatch =  new ArrayList<SysLog>();
		 for (Serializable t : ids) {
			 String ip = getIpAddr();
			 String id = t==null?"null":t.toString();
			 SysLog l=new SysLog();			 
			 l.setUserId(SubjectUtil.getCurrentUser().getUserId());
			 l.setParams("id:"+t.toString());
			 l.setLogDate(new Date());
			 l.setEventName(eventName);
			 l.setEventRecord(t.getClass().getName());
			 l.setLocal(IpUtil.getAddressByIP(ip));
			 l.setObjectId(id);
			 l.setType(type);
			 l.setIp(ip);
			 logBatch.add(l);
		}
		 session.insert(stmtId,logBatch);
	 }
	 
	 /**
	  * <p>Title: 获取删除日志</p>
	  * <p>Description:</p>
	  * <p>Company: mesnac</p>
	  * @param session
	  * @param namespace
	  * @param methodFlag
	  * @param o
	  * @param type
	  * @param stmtId
	  * @param eventName
	  * @author wuzq
	  * @date 2017-5-25上午9:16:12
	  * @retrun
	  */
	@SuppressWarnings("unchecked")
	public static void getDeleteLog(SqlSession session, String namespace , String methodFlag, Object o, Integer type,
			 String stmtId, String eventName){
			 String param = "";
		if(SYSLOGNAMESPACE.equals(namespace)){
			if(DELETEBYWHERE.equals(methodFlag)){
				param = "where:"+ o.toString();
			}
			if(DELETEBYPARAM.equals(methodFlag) || DELETEBYPRIMARYKEYS.equals(methodFlag)){
				Map<String,Object> map = (Map<String,Object>)o;
		         for (Map.Entry<String, Object> entry : map.entrySet()) {
		        	 Object value = entry.getValue();
		        	 param +=  entry.getKey()+":"+ (value==null?"null":value.toString())+";";
		           }
			}
			if(DELETEBYPRIMARYKEY.equals(methodFlag)){
				param = "id:"+o.toString();
			}
		}
		String ip = getIpAddr();
		SysLog l=new SysLog();			 
		l.setUserId(SubjectUtil.getCurrentUser().getUserId());
		l.setParams(o.toString());
		l.setLogDate(new Date());
		l.setEventName(eventName);
		l.setEventRecord(o.getClass().getName());
		l.setLocal(IpUtil.getAddressByIP(ip));
		l.setObjectId(param);
		l.setType(type);
		l.setIp(ip);
		session.insert(stmtId,l);
	 }
}
